<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\OrderLease;
use App\Services\AlipayMessageService;
use App\Services\OrderService;
use Illuminate\Console\Command;

class OverdueOrderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'overdueOrder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '逾期统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->overdueOrderCount();
    }

    public function overdueOrderCount(){
        try{
            $leaseStatus = [
                OrderLease::ORDER_LEASE_STATUS_NOPAY,
                OrderLease::ORDER_LEASE_STATUS_OVERDUE,
            ];
            $where = [
                ['o.pay_type','!=',Order::ORDER_PAY_TYPE_HUABEI],
                ['ol.withhold_status',OrderLease::LEASE_WITHHOLD_STATUS_NO],
                ['ol.begin_time','<=',strtotime(date('Y-m-d 23:59:59',strtotime('-1 day')))],
            ];
            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id','o.status as order_status','ol.id as lease_id','ol.paid_rent','ol.overdue_amount','ol.begin_time','ol.status as lease_status','ol.rent')
                ->leftjoin('sp_order_lease_2 as ol', 'o.id','ol.order_id')
                ->whereIn('o.status',OrderService::getOrderStatus(OrderService::ORDER_OVERDUE_COUNT))
                ->whereIn('ol.status',$leaseStatus)
                ->where($where)
                ->orderBy('ol.id', 'asc')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $k=>$v){
                    $overDay = ceil((strtotime(date('Y-m-d H:i:s')) - $v['begin_time']) / 86400);
                    if($overDay>=8){
                        //修改逾期天数
                        $leaseData['overdue_days'] = $overDay;
//                        $leaseData['overdue_amount'] = bcmul($v['rent']*$overDay,0.00065,2);
                        $overdue_amount = bcmul($v['rent']-$v['paid_rent']+$v['overdue_amount'],0.00065,2);
                        $leaseData['overdue_amount']=bcadd($overdue_amount,$v['overdue_amount'],2);//逾期费计算
                        if($v['lease_status'] != OrderLease::ORDER_LEASE_STATUS_OVERDUE){
                            //修改租期状态
                            $leaseData['status'] = OrderLease::ORDER_LEASE_STATUS_OVERDUE;
                        }
                        OrderLease::where(['id'=>$v['lease_id']])->update($leaseData);
                        if($v['order_status'] != OrderService::ORDER_STATUS_OVERDUE){ //overdue_days
                            //修改订单状态
                            $OrderData['status'] = OrderService::ORDER_STATUS_OVERDUE;
                            Order::where(['id'=>$v['id']])->update($OrderData);
                            $data[$k]['order_status'] = OrderService::ORDER_STATUS_OVERDUE;
                        }

                    }else if($overDay<8&&$overDay>0){
//                     if($v['order_status'] != OrderService::ORDER_STATUS_OVERDUE&&$v['order_status'] != OrderService::ORDER_STATUS_REPAYMENT){ //overdue_days
                        //修改订单状态
                        $leaseData['overdue_days'] = $overDay;
                        $overdue_amount = bcmul($v['rent']-$v['paid_rent']+$v['overdue_amount'],0.00065,2);
                        $leaseData['overdue_amount']=bcadd($overdue_amount,$v['overdue_amount'],2);//逾期费计算
                        if($v['lease_status'] != OrderLease::ORDER_LEASE_STATUS_OVERDUE){
                            //修改租期状态
                            $leaseData['status'] = OrderLease::ORDER_LEASE_STATUS_OVERDUE;
                        }
                        OrderLease::where(['id'=>$v['lease_id']])->update($leaseData);

                        if($v['order_status'] != OrderService::ORDER_STATUS_OVERDUE&&$v['order_status'] != OrderService::ORDER_STATUS_REPAYMENT){
                            $OrderData['status'] = OrderService::ORDER_STATUS_REPAYMENT;
                            Order::where(['id'=>$v['id']])->update($OrderData);
                            $data[$k]['order_status'] = OrderService::ORDER_STATUS_REPAYMENT;
                        }

//                        }
                    }
                }

                $unData = assoc_unique($data, 'id');
                if($unData){
                    foreach ($unData as $item){
                        if($item['order_status'] == OrderService::ORDER_STATUS_OVERDUE){ //overdue_days
                            //修改最大逾期天数
                            $OrderDays['overdue_days'] = OrderLease::where(['order_id'=>$item['id']])->max('overdue_days');
                            Order::where(['id'=>$item['id']])->update($OrderDays);
                            AlipayMessageService::sendChangeOrderMessage($item['id']);
                        }
                    }
                }
                \Illuminate\Support\Facades\Log::channel("cron")->debug('逾期统计执行成功',$data);
            }
        }catch (\Exception $exception){
            \Illuminate\Support\Facades\Log::channel("cron")->debug('逾期统计执行失败'.$exception->getMessage());
        }
    }





}
